/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReceitasPainel.java
 *
 * Created on 23/01/2013, 21:30:53
 */
package com.prsistema.Receitas;

import com.prsistema.utilitarias.Receita;
import com.prsistema.DAO.Conexao;
import com.prsistema.DAO.DAOReceita;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class VisualizarReceita extends javax.swing.JPanel {

    private ArrayList<Receita> listaReceitas = new ArrayList<Receita>();
    private DAOReceita daoReceita;
    private boolean novo;
    private boolean salvar;
    private boolean cancelar;

    public VisualizarReceita(Conexao con) {
        this.daoReceita = new DAOReceita(con.getConector());
        this.listaReceitas = this.daoReceita.selectAll();
        this.novo = true;
        this.salvar = false;
        this.cancelar = false;
        initComponents();
        this.carregarDados();
    }

    private void carregarDados() {
        this.listaReceitas = this.daoReceita.selectAll();
        this.preencheCampos();
    }
    
    private void preencheCampos(){
        this.listagemReceitas.removeAll();
        for (int i = 0; i < this.listaReceitas.size(); i++) {
            listagemReceitas.add(listaReceitas.get(i).getNomeProduto());
        }
    }

    private void mudaPosicao(int index) {
        Receita rc = this.listaReceitas.get(index);

        this.txtCodigo.setText(rc.getIdRec());
        this.txtProduto.setText(rc.getNomeProduto());
        this.txtInfo.setText(rc.getInfoRec());

        this.btnAtualizar.setEnabled(true);
        this.btnDeletar.setEnabled(true);
    }

    private void cancelar() {
        //Deixa todos os campos em branco
        this.txtCodigo.setText("");
        this.txtProduto.setText("");
        this.txtInfo.setText("");
        this.txtPesquisar.setText("");

        //Abilita botões 
        this.btnAnterior.setEnabled(true);
        this.btnProximo.setEnabled(true);
        this.listagemReceitas.setEnabled(true);
        this.txtPesquisar.setEditable(true);
        this.btnAtualizar.setEnabled(false);

        //Desabilita campos de texto
        this.txtCodigo.setEditable(false);
        this.txtProduto.setEditable(false);
        this.txtInfo.setEditable(false);

        //Muda status de salvar para novo
        this.btnNovo.setText("Novo");
        this.btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnAdicionar.png")));
        this.btnAtualizar.setText("Atualizar");
        this.btnAtualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnAtualizar1.png")));

        this.carregarDados();
    }

    private boolean salvar() {
        boolean retorno = false;

        try {
            Receita rc = new Receita();

            rc.setIdRec(this.txtCodigo.getText());
            rc.setNomeProduto(this.txtProduto.getText());
            rc.setInfoRec(this.txtInfo.getText());

            if ("".equals(rc.getIdRec()) || "".equals(rc.getNomeProduto())) {
                JOptionPane.showMessageDialog(null, "Preencha os campos código e produto");
            } else {
                Receita r = this.daoReceita.select(rc.getNomeProduto());

                if (this.novo) {
                    if (rc.getNomeProduto().equals(r.getNomeProduto())) {
                        JOptionPane.showMessageDialog(null, "Já existe uma receita cadastrada para este produto");
                    } else {
                        if (this.daoReceita.insert(rc)) {
                            JOptionPane.showMessageDialog(null, "Receita cadastrada com sucesso");
                            retorno = true;
                            this.cancelar();
                        }
                    }
                } else {

                    if (rc.getNomeProduto().equals(r.getNomeProduto()) && 
                            !rc.getNomeProduto().equals(this.listaReceitas.get(this.listagemReceitas.getSelectedIndex()).getNomeProduto())) {
                        JOptionPane.showMessageDialog(null, "Já existe uma receita cadastrada para este produto");
                    } else {
                        r = this.listaReceitas.get(this.listagemReceitas.getSelectedIndex());
                        if (this.daoReceita.update(rc, r.getIdRec())) {
                            JOptionPane.showMessageDialog(null, "Receita atualizada com sucesso");
                            retorno = true;
                            this.cancelar();
                        }
                    }
                }
            }

            return retorno;
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            return false;
        }
    }

    private void novoAtuali() {
        if (this.novo) {
            //Deseleciona item na listagem
            this.listagemReceitas.deselect(this.listagemReceitas.getSelectedIndex());

            this.btnAtualizar.setEnabled(true);
            //Desabilita campos de texto
            this.txtCodigo.setText("");
            this.txtProduto.setText("");
            this.txtInfo.setText("");
        }

        this.btnAnterior.setEnabled(false);
        this.btnProximo.setEnabled(false);
        this.btnDeletar.setEnabled(false);
        this.listagemReceitas.setEnabled(false);

        this.txtPesquisar.setEditable(false);
        this.txtCodigo.setEditable(true);
        this.txtProduto.setEditable(true);
        this.txtInfo.setEditable(true);

        //Muda status de novo para salvar
        this.btnNovo.setText("Salvar");
        this.btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnSalvar.png")));
        this.btnAtualizar.setText("Cancelar");
        this.btnAtualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnCancelar.png")));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        listagemReceitas = new java.awt.List();
        jPanel3 = new javax.swing.JPanel();
        btnNovo = new javax.swing.JButton();
        btnAtualizar = new javax.swing.JButton();
        btnDeletar = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        btnProximo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtInfo = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        txtProduto = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtPesquisar = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "RECEITAS CADASTRADAS", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 13))); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(240, 460));

        listagemReceitas.setFont(new java.awt.Font("Calibri", 0, 13));
        listagemReceitas.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                listagemReceitasItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listagemReceitas, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(listagemReceitas, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BOTÕES DE CONTROLE", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 13))); // NOI18N

        btnNovo.setFont(new java.awt.Font("Calibri", 0, 13));
        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnAdicionar.png"))); // NOI18N
        btnNovo.setText("Novo");
        btnNovo.setMaximumSize(new java.awt.Dimension(111, 35));
        btnNovo.setMinimumSize(new java.awt.Dimension(111, 35));
        btnNovo.setPreferredSize(new java.awt.Dimension(111, 35));
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnAtualizar.setFont(new java.awt.Font("Calibri", 0, 13));
        btnAtualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnAtualizar1.png"))); // NOI18N
        btnAtualizar.setText("Atualizar");
        btnAtualizar.setEnabled(false);
        btnAtualizar.setMaximumSize(new java.awt.Dimension(111, 35));
        btnAtualizar.setMinimumSize(new java.awt.Dimension(111, 35));
        btnAtualizar.setPreferredSize(new java.awt.Dimension(111, 37));
        btnAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtualizarActionPerformed(evt);
            }
        });

        btnDeletar.setFont(new java.awt.Font("Calibri", 0, 13));
        btnDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnExcluir.png"))); // NOI18N
        btnDeletar.setText("Deletar");
        btnDeletar.setEnabled(false);
        btnDeletar.setMaximumSize(new java.awt.Dimension(111, 35));
        btnDeletar.setMinimumSize(new java.awt.Dimension(111, 35));
        btnDeletar.setPreferredSize(new java.awt.Dimension(111, 35));
        btnDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletarActionPerformed(evt);
            }
        });

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnSetaAnterior.png"))); // NOI18N
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        btnProximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnSetaProximo.png"))); // NOI18N
        btnProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProximoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAnterior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAtualizar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnProximo)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnProximo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addComponent(btnAnterior, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                        .addComponent(btnAtualizar, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                        .addComponent(btnDeletar, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "INFORMAÇÕES DE RECEITA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 13))); // NOI18N

        txtInfo.setColumns(20);
        txtInfo.setEditable(false);
        txtInfo.setFont(new java.awt.Font("Calibri", 0, 13));
        txtInfo.setRows(5);
        jScrollPane1.setViewportView(txtInfo);

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel3.setText("Informações Adicionais");

        txtProduto.setEditable(false);
        txtProduto.setFont(new java.awt.Font("Calibri", 0, 13));

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel1.setText("Produto");

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel2.setText("Código");

        txtCodigo.setEditable(false);
        txtCodigo.setFont(new java.awt.Font("Calibri", 0, 13));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtProduto, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel4.setText("Pesquisar");

        txtPesquisar.setFont(new java.awt.Font("Calibri", 0, 13));
        txtPesquisar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPesquisarKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        jLabel5.setText("Staus...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
    if (this.salvar) {
        if (this.salvar()) {
            this.cancelar = false;
            this.salvar = false;
        }
    } else {
        this.salvar = true;
        this.novo = true;
        this.cancelar = true;
        this.novoAtuali();
    }
}//GEN-LAST:event_btnNovoActionPerformed

private void listagemReceitasItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_listagemReceitasItemStateChanged
    int index = this.listagemReceitas.getSelectedIndex();
    this.mudaPosicao(index);
}//GEN-LAST:event_listagemReceitasItemStateChanged

private void btnAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtualizarActionPerformed
    if (this.cancelar) {
        this.cancelar();
        this.cancelar = false;
        this.salvar = false;
    } else {
        this.novo = false;
        this.cancelar = true;
        this.salvar = true;
        this.novoAtuali();
    }
}//GEN-LAST:event_btnAtualizarActionPerformed

private void btnDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletarActionPerformed
    int opt = JOptionPane.showConfirmDialog(null, "Deseja mesmo excluir esta receita?");
    Receita rc = this.listaReceitas.get(this.listagemReceitas.getSelectedIndex());

    if (opt == 0) {
        if (this.daoReceita.delete(rc)) {
            JOptionPane.showMessageDialog(null, "Receita deletada com sucesso");
            this.carregarDados();

            this.btnDeletar.setEnabled(false);
            this.txtCodigo.setText("");
            this.txtProduto.setText("");
            this.txtInfo.setText("");
        }
    }
}//GEN-LAST:event_btnDeletarActionPerformed

private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProximoActionPerformed
    int index = this.listagemReceitas.getSelectedIndex();

    if (index == -1 || index == this.listaReceitas.size() - 1) {
        this.mudaPosicao(0);
        this.listagemReceitas.select(0);
    } else {
        this.mudaPosicao(index + 1);
        this.listagemReceitas.select(index + 1);
    }
}//GEN-LAST:event_btnProximoActionPerformed

private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
    int index = this.listagemReceitas.getSelectedIndex();

    if (index == -1) {
        this.mudaPosicao(0);
        this.listagemReceitas.select(0);
    } else {
        if (index == 0) {
            this.mudaPosicao(this.listaReceitas.size() - 1);
            this.listagemReceitas.select(this.listaReceitas.size() - 1);
        } else {
            this.mudaPosicao(index - 1);
            this.listagemReceitas.select(index - 1);
        }
    }
}//GEN-LAST:event_btnAnteriorActionPerformed

private void txtPesquisarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPesquisarKeyReleased
    this.listaReceitas = this.daoReceita.selectLike(this.txtPesquisar.getText());
    this.preencheCampos();
}//GEN-LAST:event_txtPesquisarKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnAtualizar;
    private javax.swing.JButton btnDeletar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnProximo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.List listagemReceitas;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextArea txtInfo;
    private javax.swing.JTextField txtPesquisar;
    private javax.swing.JTextField txtProduto;
    // End of variables declaration//GEN-END:variables
}
